const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    list: 4,
    listItemSelected: [],
    showNoData: false,
    orderNum: 0
  },
  onLoad(options){
    if (options.CarInfo){
      var CarInfo = JSON.parse(options.CarInfo);
      this.setData({
        CarInfo: CarInfo
      });
    }
    if(options.search_val){
      this.search(options.search_val);
    }else{
      this.getParts({
        brandCode: CarInfo.brandCode,
        vehicleId: CarInfo.vehicleId,
        partGroupId: options.id
      });
    }
    
  },
  onShow(){
    this.getCart();  
  },

  /*分享*/
  onShareAppMessage(){} ,

  /**
   * @explain 搜索
   * @param oe 搜索OE号
   * 
   * */ 
  search(search_val){
    wx.showNavigationBarLoading();
    let _this = this, td = _this.data;

    SERVICE.POST(SERVICE.URL.OESearch, {
      thirdCookie: wx.getStorageSync("thirdCookie"),
      oe: search_val
    }, (status, res) => {
      wx.hideNavigationBarLoading();
      
      if(res.data.status == "success" && res.data.res_msg.result !== null){
        let list = res.data.res_msg.result.partsList;
        for (let i = 0; i < list.length; i++) {
          td.listItemSelected.push({
            selected: false,
            imgSelected: false            
          });
        }
        _this.setData({
          list
        });

        _this.getCart();
      }else{
        _this.setData({
          list: 0,
          showNoData: true
        });
      }
    });
  },

  /**
   * @explain 获取配件类表
   * @param brandCode 品牌
   * @param vehicleId 车型ID
   * @param partGroupId 品牌ID
   * 
   */
  getParts(params) {
    wx.showNavigationBarLoading();
    let _this = this, td = _this.data;
    params.thirdCookie = wx.getStorageSync("thirdCookie"); 
       
    SERVICE.POST(SERVICE.URL.getParts, params, (status, res) => {
      wx.hideNavigationBarLoading();
      console.log("获取配件列表:",res);
      if(res.data.status == "success"){
        let list = res.data.res_msg.result.partsList;
        for (let i = 0;i < list.length; i++){
          td.listItemSelected.push({
            selected: false,
            imgSelected: false
          });
        }
        _this.setData({
          list
        });
        
      }else{
        wx.showToast({
          title: "获取配件列表失败",
          icon: "none",
          duration: 1500,
          success:function(){
            _this.setData({
              list: 0
            });
          }
        });
      }
    });
  },

  /**
   *@explain 显示隐藏列表项详情 
   * 
   */ 
  detail(e){
    let td = this.data, 
    index = e.currentTarget.dataset.index,
    item_data = e.currentTarget.dataset.item,
    price = e.currentTarget.dataset.price;
 
    if(price == 1){
      td.listItemSelected[index].selected = !td.listItemSelected[index].selected;
      // td.listItemSelected[index].imgSelected = !td.listItemSelected[index].imgSelected;
      
      this.setData({
        listItemSelected: td.listItemSelected
      });
    }else if(price == 0){
      this.getPartsPrice(index, {
        oeId: item_data.oeId,
        brandId: td.CarInfo.brandId
      });
    }
    
  },

  /**
   * @explain 获取配件价格
   * @param partGroupId 品牌ID
   * @param oeId 原零件ID
   * **/ 
  getPartsPrice(index,params,fun){
    wx.showLoading({
      title: "加载中...",
      mask: true
    });
    let _this = this, td = _this.data;
    SERVICE.POST(SERVICE.URL.getPartsPrice, params, (status, res) => {
      wx.hideLoading();
      if(res.data.status == "success"){
        let price = res.data.part;
        td.listItemSelected[index].selected = !td.listItemSelected[index].selected;
        

        for(let i = 0;i < td.list.length; i++){
          if (td.list[i].oeId == price.oeId){
            td.list[i].price = price;
          }
        }
        _this.setData({
          list: td.list,
          listItemSelected: td.listItemSelected
        });
        typeof fun === "function" && fun(price);
      }else{
        wx.showToast({
          title: "加载失败...",
          icon: "none",
          mask: true
        });
      }
    });
  },

  /**
   * @explain 加入购物车
   * 
   * **/ 
  addCart(e){
    let _this = this, td = _this.data, 
      item_data = e.currentTarget.dataset.item,
      price = e.currentTarget.dataset.price, 
      index = e.currentTarget.dataset.index;

    td.listItemSelected[index].imgSelected = !td.listItemSelected[index].imgSelected;
      this.setData({
        listItemSelected: td.listItemSelected
      });
    if (td.CarInfo){
      var Cart_params = {
        parts: JSON.stringify(item_data),
        price: JSON.stringify(price),
        CX: td.CarInfo.brandName,
        vin: wx.getStorageSync("VIN")
      };
    }else{
      var Cart_params = {
        parts: JSON.stringify(item_data),
        price: JSON.stringify(price),
        CX: "--",
        vin: item_data.brandName,
      }; 
    }
   
  
    if(price){
      _this.Cart(Cart_params);
    }else{
      this.getPartsPrice(index, {
        oeId: item_data.oeId,
        brandId: td.CarInfo.brandId
      }, (price) => {
        Cart_params.price = JSON.stringify(price);
        _this.Cart(Cart_params);
      });
    }
  },

  /**
   * @explain 加入购物车
   * @params thirdCookie  用户信息
   * @params parts  配件信息
   * @params price 价格
   * @params CX 车型
   * @params VIN vin码
   * 
   * **/
  Cart(params){
    wx.showLoading({
      title: "加入购物车中...",
      mask: true
    });
    let _this = this;
    params.thirdCookie = wx.getStorageSync("thirdCookie");
    SERVICE.POST(SERVICE.URL.addshoping, params, (status, res) => {
      wx.hideLoading();
      if(res.data.status == "success"){
        wx.showToast({
          title: "加入购物车成功",
          icon: "none",
          mask: true,
          success:function(){
            _this.getCart();
          }
        });
      }else{
        wx.showToast({
          title: "加入购物车失败",
          icon: "none",
          mask: true
        });
      }
    });
  },

  getCart(){
    let _this = this;
    SERVICE.POST(SERVICE.URL.getCart,{
      thirdCookie: wx.getStorageSync("thirdCookie")
    },(status,res)=>{
      _this.setData({
        orderNum: res.data.count
      });
    });
  },

  getImg(e){
    let _this = this, td = this.data,
     oeId = e.currentTarget.dataset.oeid,
     index = e.currentTarget.dataset.index,
      item_data = e.currentTarget.dataset.item,     
      price = e.currentTarget.dataset.price;
    wx.showLoading({
      title: "获取图片中...",
      mask: true
    });
    SERVICE.POST(SERVICE.URL.oeIdGetPartsPicture,{
    brandCode: td.CarInfo.brandCode,
    vehicleId: td.CarInfo.vehicleId,
      oeId: oeId
    },(status,res)=>{
      console.log("获取图片中:",res);
      wx.hideLoading();
      if(res.data.status == "success"){
        td.list[index].imgSrc = res.data.imgSrc;
        _this.setData({
          list: td.list
        });
        wx.previewImage({
          urls: [res.data.imgSrc],
          success: function () {
          }
        })
      }else{
        wx.showToast({
          title: "抱歉，此部件暂无结构图片",
          icon:"none",
          mask: true
        });
      }
    });
  },
  view(e){
    console.log(e);
  }
});